﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;

namespace ClassLibrary
{
    public class XMLSerialisation
    {
        /*Exemple
           Person p = new Person();
           p.FirstName = "Joe";
           p.LastName = "Bloggs";
           p.Age = 40;

           XmlSerialisation.Save<Person>("joebloggs.xml", p);
           Person p = XmlSerializerUtil.Load<Person>("joebloggs.xml");          
       */
        public static T Load<T>(string path)
        {
            T savedObject = default(T);
            XmlSerializer x = new XmlSerializer(typeof(T));
            using (FileStream st = File.Open(path, FileMode.Open))
            {
                savedObject = (T)x.Deserialize(st);
            }
            return savedObject;
        }

        public static void Save<T>(string path, T obj)
        {
            XmlSerializer x = new XmlSerializer(typeof(T));

            using (FileStream st = File.Open(path, FileMode.Create))
            {
                x.Serialize(st, obj);
            }
        }
    }
}
